package com.liuguang.testassert.congfig;

import com.liuguang.testassert.common.ErrorResponse;
import com.liuguang.testassert.common.exception.ParamIsNullException;
import com.liuguang.testassert.enums.ResultFlag;
import com.liuguang.testassert.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: ControllerAdvice
 * @Description：
 * @Author: wanghr
 * Date: 1:54 PM 2020/5/30
 * Version: 1.0
 */
@Component
@ControllerAdvice(basePackages = "com.liuguang.testassert.com.liuguang.controller")
public class ControllerAdviceConfig {
    private final static Logger logger = LoggerFactory.getLogger(ControllerAdviceConfig.class);


    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public Result<String> exceptionHandler(IllegalArgumentException exception) {
        Result<String> result = new Result<>();
        String message = exception.getMessage();
        result.setCode(ResultFlag.FAIL.getCode());
        result.setMsg(message);
        return result;
    }

    @ExceptionHandler(value = ParamIsNullException.class)
    @ResponseBody
    public ErrorResponse paramExceptionhandler(ParamIsNullException e) {
        logger.info("传入参数异常：{}",e.getMessage());
        String code = e.getCode();
        System.out.println("code = " + code);
        return new ErrorResponse(e.getCode(),e.getMessage());
    }
}
